package com.ruoyi.project.labappointment.controller;

import com.alibaba.fastjson.JSONArray;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.lab.domain.LabAppointmentDto;
import com.ruoyi.project.labappointment.domain.AddList;
import com.ruoyi.project.labappointment.domain.AddListParam;
import com.ruoyi.project.labappointment.domain.ApplyParam;
import com.ruoyi.project.labappointment.mapper.LabAppointmentMapper;
import com.ruoyi.project.labappointment.service.LabAppointmentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

/**
 * 【实验室预约】Controller
 * 
 * @author ruoyi
 * @date 2019-11-16
 */
@Controller
@RequestMapping("labappointment")
public class LabAppointmentController extends BaseController
{
    private String prefix = "system/labappointment";

    @Autowired
    private LabAppointmentService labAppointmentService;

    @Autowired
    private LabAppointmentMapper labAppointmentMapper;

    @RequiresPermissions("labappointment:view")
    @GetMapping()
    public String property()
    {
        return prefix + "/labappointment";
    }

    /**
     * 查询【教师管理：实验室预约列表】列表
     */
    @RequiresPermissions("system:labapprove:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AddListParam addListParam)
    {
        startPage();
        List<LabAppointmentDto> labAppointmentDtos =labAppointmentService.selectAppointemntSelect(addListParam,getUserId());
        return getDataTable(labAppointmentDtos);
    }

    /**
     * 跳转到节次日期列表
     * @param mmap
     * @return
     */
    @GetMapping(value = "/addlist")
    public String addlist(ModelMap mmap)
    {
        /**实验室id   和实验日期两个字段*/
        AddListParam addListParam=new AddListParam();
        /*得到所有的启用的实验室id*/
        addListParam.setLabId(labAppointmentMapper.selectFirstLabId());

        List<AddList> addLists=labAppointmentService.selectAddList(addListParam);
        int i=0;
        for(AddList addList:addLists){
            Calendar c = Calendar.getInstance();
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            try {
                c.setTime(format.parse(addList.getSecDate()));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            c.add(Calendar.DATE, i);
            addList.setSecDate(format.format(c.getTime()));
            i++;
        }
        mmap.put("addList", addLists);
        mmap.put("labInfo", addListParam);
        return prefix + "/add";
    }

    /**
     * 实验室预约节次列表
     * @param addListParam
     * @param mmap
     * @return
     */
    @PostMapping(value = "/addlists")
    public String addlists(AddListParam addListParam,ModelMap mmap)
    {
        List<AddList>addLists=labAppointmentService.selectAddList(addListParam);
        mmap.put("addList", addLists);
        mmap.put("labInfo", addListParam);
        return prefix + "/add";
    }

    /**
     * 撤销审核
     * @param id
     * @return
     */
    @GetMapping(value = "/quitApply")
    @ResponseBody
    public AjaxResult quitApply(Long id)
    {
        return labAppointmentService.quitApply(id);
    }


    /**
     * 申请实验室
     * @param applyParam
     * @return
     */
    @PostMapping(value = "/apply")
    @ResponseBody
    public AjaxResult apply(@RequestBody String  applyParam)
    {
        if(applyParam==null){
            return new AjaxResult(AjaxResult.Type.ERROR,"参数错误");
        }
        List<ApplyParam> taskNodes = JSONArray.parseArray(applyParam,ApplyParam.class);
        return labAppointmentService.applyLab(taskNodes,getUserId());
    }


    /**
     * 拒绝申请
     */
    @RequiresPermissions("system:labappointment:edit")
    @PostMapping("/refuse")
    @ResponseBody
    public AjaxResult refuse(Long id){
        System.err.println("id:"+id);
        return toAjax(labAppointmentMapper.refuse(id));
    }


    /**
     * 通过申请
     */
    @RequiresPermissions("system:labappointment:edit")
    @PostMapping("/goOut")
    @ResponseBody
    public AjaxResult goOut(Long id){
        return toAjax(labAppointmentMapper.goOut(id));
    }


    /**
     * 删除实验室预约
     */
    @RequiresPermissions("system:labappointment:remove")
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        //System.err.println(ids);
        //return toAjax(true);
        return toAjax(labAppointmentService.deleteAppoinment(ids));
    }


    @RequiresPermissions("system:labappointment:edit")
    @GetMapping("/edit/{id}")
    public String getAppointment(@PathVariable Long id,ModelMap mmap){
        //System.err.println(id);
        /**实验室id   和实验日期两个字段*/
        AddListParam addListParam=new AddListParam();
        /*得到所有的启用的实验室id*/
        addListParam.setLabId(labAppointmentMapper.selectFirstLabId());

        List<AddList> addLists=labAppointmentService.selectAddList(addListParam);
        int i=0;
        for(AddList addList:addLists){
            Calendar c = Calendar.getInstance();
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            try {
                c.setTime(format.parse(addList.getSecDate()));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            c.add(Calendar.DATE, i);
            addList.setSecDate(format.format(c.getTime()));
            i++;
        }
        addListParam.setId(id);
        mmap.put("addList", addLists);
        mmap.put("labInfo", addListParam);
        return prefix+"/edit";
    }

    @PostMapping(value = "/edits")
    public String edits(AddListParam addListParam,ModelMap mmap)
    {
        List<AddList>addLists=labAppointmentService.selectAddList(addListParam);
        mmap.put("addList", addLists);
        mmap.put("labInfo", addListParam);
        return prefix + "/edit";
    }

    /**
     * 修改保存
     * @param applyParam
     * @return
     */
    @PostMapping(value = "/apply2")
    @ResponseBody
    public AjaxResult apply2(@RequestBody String  applyParam)
    {
        //System.err.println(applyParam);
       //修改保存  可以先删除已有的，然后再重新添加
        if(applyParam==null){
            return new AjaxResult(AjaxResult.Type.ERROR,"参数错误");
        }
        List<ApplyParam> taskNodes = JSONArray.parseArray(applyParam,ApplyParam.class);
        String id=taskNodes.get(0).getId().toString();
        labAppointmentService.deleteAppoinment(id);
        return labAppointmentService.applyLab(taskNodes,getUserId());
    }
}
